package cn.skcks.springboot05mybatis.controller;

import cn.skcks.springboot05mybatis.dao.UserDao;
import cn.skcks.springboot05mybatis.pojo.User;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
@Api(tags = {"用户信息"})
public class UserController {

	private UserDao userDao;

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@GetMapping(value = "/list", produces = {MediaType.APPLICATION_JSON_VALUE})
	public List<User> getUserList() {
		List<User> list = userDao.getUserList();
		for (User user : list) {
			System.out.println(user);
		}
		return list;
	}

	@GetMapping(value = "/id/{id}",produces = {MediaType.APPLICATION_JSON_VALUE,MediaType.TEXT_PLAIN_VALUE})
//	Swagger2 风格
//	@ApiOperation(value = "获取用户信息", notes = "id 为用户id")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "id",
//					value = "用户id",
//					dataTypeClass = Integer.class,
//					paramType = "path",
//					example = "1",
//					required = true)
//	})

//	Swagger3 风格
	@Operation(summary = "获取用户信息",description = "id 为用户 id")
	@Parameters({
		@Parameter(name = "id",
				description = "用户id",
				in = ParameterIn.PATH,
				example = "1",
				required = true
		)
	})
	//	public User getUserById(
	//			@ApiParam(name = "id", value = "用户id", required = true)
	//			@PathVariable("id") int id) {
	public User getUserById(@PathVariable("id") int id) {
		return userDao.getUserById(id);
	}

	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/add")
	public String addUser() {
		User user = new User(1000, "SpringBoot", "12341234");
		int res = userDao.addUser(user);
		if (res > 0) {
			return "添加成功";
		} else {
			return "添加失败";
		}
	}

	@RequestMapping(method = {RequestMethod.GET, RequestMethod.PUT}, value = "/mod")
	public String updateUser() {
		User user = new User(1000, "SpringBoot", "43214321");
		int res = userDao.modUser(user);
		if (res > 0) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}

	@RequestMapping(method = {RequestMethod.GET, RequestMethod.DELETE}, value = "/del")
	public String deleteUser() {
		int res = userDao.delUser(1000);
		if (res > 0) {
			return "删除成功";
		} else {
			return "删除失败";
		}
	}
}